// 获取地址里的项目id
export function parseProjectId(): string {
	let projectId = '';
	const { pathname } = window.location;
	const reg = /^\/graphics-engine\/\d*\//;
	const result: string[] = pathname.match(reg) || [];
	if (result.length) {
		const strArr = result[0].split('/');
		projectId = strArr[2] as string;
	}
	return projectId;
}

export function ifDesign(): boolean {
	const { search } = window.location;
	const regex = /env=dev/;
	return regex.test(search);
}

export const getLocalstorage = (key: string) => {
	if (!ifDesign()) {
		const projectId = parseProjectId();
		return projectId ? localStorage.getItem(`${projectId}-${key}`) : localStorage.getItem(key);
	} else {
		return localStorage.getItem(key);
	}
};

export const setLocalstorage = (key: string, value: string) => {
	const projectId = parseProjectId();
	// eslint-disable-next-line no-unused-expressions
	projectId ? localStorage.setItem(`${projectId}-${key}`, value) : localStorage.setItem(key, value);
};
export const removeLocalStorage = (key: string) => {
	const projectId = parseProjectId();
	// eslint-disable-next-line no-unused-expressions
	projectId ? localStorage.removeItem(`${projectId}-${key}`) : localStorage.removeItem(key);
};
export const getSessionStorage = (key: string) => {
	const projectId = parseProjectId();
	return projectId ? sessionStorage.getItem(`${projectId}-${key}`) : sessionStorage.getItem(key);
};

export const setSessionStorage = (key: string, value: string) => {
	const projectId = parseProjectId();
	// eslint-disable-next-line no-unused-expressions
	projectId
		? sessionStorage.setItem(`${projectId}-${key}`, value)
		: sessionStorage.setItem(key, value);
};

export const removeSessionStorage = (key: string) => {
	const projectId = parseProjectId();
	// eslint-disable-next-line no-unused-expressions
	projectId ? sessionStorage.removeItem(`${projectId}-${key}`) : sessionStorage.removeItem(key);
};
